<?php
//die(get_env('APPLICATION_ENV'));
// Define path to application directory
error_reporting(E_ALL ^ E_NOTICE);

defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

    // Define path to public directory
defined('PUBLIC_PATH')
    || define('PUBLIC_PATH', realpath(dirname(__FILE__)));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

if (APPLICATION_ENV == 'development') {
    $startTime = microtime(true);
}

if (get_magic_quotes_gpc()) {
    function array_map_recursive($function, $arr) {
	    $tempData = array();
	    foreach($arr as $key => $value){
	        $tempData[$key] = (is_array($value) ? array_map_recursive($function, $value) : $function($value));
        }
	    return $tempData;
    }

    $_GET     = array_map_recursive('stripslashes', $_GET);
    $_POST    = array_map_recursive('stripslashes', $_POST);
    $_REQUEST = array_map_recursive('stripslashes', $_REQUEST);
}

define('APPLICATION_START_MICROTIME', microtime(true));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';
require_once 'Icygen/Config/Xml.php';

// Create application, bootstrap, and run

$config = new Icygen_Config_Xml(APPLICATION_PATH . '/configs/application.xml',
                                APPLICATION_ENV);
//echo '<pre>'.var_export($config, true); die();
$application = new Zend_Application(
    APPLICATION_ENV,
    $config /*new Icygen_Config_Xml(APPLICATION_PATH . '/configs/application.xml'*/
);

if (!ob_start("ob_gzhandler")) ob_start();

try {
    $application->bootstrap()
                ->run();
    //Zend_Debug::dump($application->getBootstrap()->getResource('Db')->getProfiler()->getQueryProfiles());
} catch (Exception $e) {
    if (APPLICATION_ENV == 'development') {
        echo '<h2>'.$e->getMessage().'</h2>';
        echo '<pre>'.$e->getTraceAsString().'</pre>';
    } else {
        echo '<h1>Site temporary unavailable</h1>';
        echo '<!--'.$e->getMessage().'-->';


    }
}

//echo '<div style="position: absolute; bottom:10px; right:10px;">' . sprintf('%.6f', microtime(true) - APPLICATION_START_MICROTIME) . '</div>';
